
/*********************************************************************
 * INCLUDES
 */

#include "gd32e23x.h"
#include "gd32e230c_eval.h"
#include "systick.h"
#include <stdint.h>
#include <stdbool.h>


#include "hal_debug.h"
#include "driver_gpio.h"


 /*********************************************************************
  * PUBLIC FUNCTIONS
  */
uint32_t driver_gpio_init(void)
{
    uint32_t err_code = 0;

    /* Output pins */
    /* enable the led2 GPIO clock */
    rcu_periph_clock_enable(RCU_GPIOB);
    /* configure led2 GPIO port */ 
    gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_4);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
    /* reset led2 GPIO pin */
    gpio_bit_reset(GPIOB, GPIO_PIN_4);
    
    
    
    
    /* enable the Tamper key GPIO clock */
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_CFGCMP);
    /* configure button pin as input */
    gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO_PIN_1);
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
    
    gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO_PIN_4);
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
    
    gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO_PIN_5);
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5);

    
    /* enable the Tamper key GPIO clock */
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_CFGCMP);
    /* configure button pin as input */
    gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_PULLDOWN, GPIO_PIN_0);
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
    
    
//    /* enable and set key EXTI interrupt priority */
//    nvic_irq_enable(EXTI0_1_IRQn,1U);
//    /* connect key EXTI line to key GPIO pin */
//    syscfg_exti_line_config(EXTI_SOURCE_GPIOA, EXTI_SOURCE_PIN1);
//    /* configure key EXTI line */
//    exti_init(EXTI_1, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
//    exti_interrupt_flag_clear(EXTI_1);
    
    return err_code;
}




uint8_t button_scan_loop(void)
{
    static uint8_t  press_flag = 0;
    static uint16_t long_press_count = 0;
    
    /* check whether the button is pressed */
    if(RESET == gpio_input_bit_get(GPIOA, GPIO_PIN_1))
    {
        /* delay 100ms for software removing jitter */
        delay_1ms(1);

        /* check whether the button is pressed */
        if(RESET == gpio_input_bit_get(GPIOA, GPIO_PIN_1))
        {
            long_press_count++;
                       
            if(long_press_count >= 5000)
            {
                if(long_press_count%1000 == 0)
                {
                    printf("\r\n\r\nkey1 long press 0x11\r\n");
                    return 0x11;
                }
            }

        }
        else
        {
            if((long_press_count > 20) && (long_press_count < 5000))
            {
                printf("\r\n\r\nkey1 press \r\n");
            
                return 1;
            }
        
        }
    } 
    else if(SET == gpio_input_bit_get(GPIOA, GPIO_PIN_0))
    {
        /* delay 100ms for software removing jitter */
        delay_1ms(1);
       
        /* check whether the button is pressed */
        if(SET == gpio_input_bit_get(GPIOA, GPIO_PIN_0))
        {
            long_press_count++;
                       
            if(long_press_count >= 5000)
            {
                if(long_press_count%1000 == 0)
                {
                    printf("\r\n\r\nkey2 long press 0x12\r\n");
                    return 0x12;
                }
            }

        }
        else
        {
            if((long_press_count > 20) && (long_press_count < 5000))
            {
                printf("\r\n\r\nkey2 press \r\n");
            
                return 2;
            }
        
        }
    }

    else if(RESET == gpio_input_bit_get(GPIOA, GPIO_PIN_4))
    {
        /* delay 100ms for software removing jitter */
        delay_1ms(1);

        /* check whether the button is pressed */
        if(RESET == gpio_input_bit_get(GPIOA, GPIO_PIN_4))
        {
            long_press_count++;
                       
            if(long_press_count >= 5000)
            {
                if(long_press_count%1000 == 0)
                {
                    printf("\r\n\r\nkey3 long press 0x33\r\n");
                    return 0x33;
                }
            }

        }
        else
        {
            if((long_press_count > 20) && (long_press_count < 5000))
            {
                printf("\r\n\r\nkey3 press \r\n");
            
                return 3;
            }
        
        }
    } 

    else if(RESET == gpio_input_bit_get(GPIOA, GPIO_PIN_5))
    {
        /* delay 100ms for software removing jitter */
        delay_1ms(1);

        /* check whether the button is pressed */
        if(RESET == gpio_input_bit_get(GPIOA, GPIO_PIN_5))
        {
            long_press_count++;
                       
            if(long_press_count >= 5000)
            {
                if(long_press_count%1000 == 0)
                {
                    printf("\r\n\r\nkey4 long press 0x44\r\n");
                    return 0x44;
                }
            }

        }
        else
        {
            if((long_press_count > 20) && (long_press_count < 5000))
            {
                printf("\r\n\r\nkey4 press \r\n");
            
                return 4;
            }
        
        }
    }     
    else
    {
        press_flag = 0;
        long_press_count = 0;
    }
    
    return 0;
}